package com.example.certificate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.dto.PerfectInfoDto;
import com.example.certificate.dto.RegisterDto;
import com.example.certificate.dto.common.UserBasePageDto;
import com.example.certificate.entity.OrgUser;
import com.example.certificate.entity.User;
import com.example.certificate.execption.BusinessException;
import com.example.certificate.mapper.OrgUserMapper;
import com.example.certificate.mapper.UserMapper;
import com.example.certificate.service.OrgUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.certificate.utils.ResultConstant;
import com.example.certificate.vo.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 */
@Service
public class OrgUserServiceImpl extends ServiceImpl<OrgUserMapper, OrgUser> implements OrgUserService {

    @Resource
    private OrgUserMapper orgUserMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void perfectUserData(PerfectInfoDto dto) throws BusinessException {
        Boolean userExists = false;
        try {
            userExists = checkUserExists(dto.getPhone());
            if(userExists){
                orgUserMapper.updateById(getOrgUser(dto));
                User user = new User();
                user.setRoleId(dto.getIdentityCard());
                userMapper.update(user, new QueryWrapper<User>().eq("phone", dto.getPhone()));
            }else {
                throw new BusinessException(ResultConstant.IS_EXIST.resultCode,"该用户不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Page<OrgUser> selBasePage(UserBasePageDto dto) {
        Page<OrgUser> page = new Page<>();
        page.setSize(dto.getPageSize() == null ? 10 : dto.getPageSize());
        page.setCurrent(dto.getCurrentPage() == null ? 1 : dto.getCurrentPage());
        return orgUserMapper.selBasePage(page, dto);
    }

    @Override
    public Page<OrgUser> selectManagement(UserBasePageDto dto) {
        Page<OrgUser> page = new Page<>();
        page.setSize(dto.getPageSize() == null ? 10 : dto.getPageSize());
        page.setCurrent(dto.getCurrentPage() == null ? 1 : dto.getCurrentPage());
        return orgUserMapper.selManagement(page, dto);
    }

    @Override
    public Boolean updateOrgUser(OrgUser dto) {
        return orgUserMapper.updateById(dto) > 0 ? true : false;
    }

    @Override
    public UserInfoVo getUserBase(String userId) {
        return orgUserMapper.selById(userId);
    }

    /**
     * @Description 匹配需要更新的用户数据
     * @Param [dto]
     * @return com.han.nternational.domain.entity.OrgUser
     **/
    public OrgUser getOrgUser(PerfectInfoDto dto){
        OrgUser orgUser = orgUserMapper.selectOne(new QueryWrapper<OrgUser>().eq("phone", dto.getPhone()));
        BeanUtils.copyProperties(dto,orgUser);
        return orgUser;
    }

    /**
     * @Description  校验用户数据是否存在
     * @Param [phone]
     * @return java.lang.Boolean
     **/
    public Boolean checkUserExists(String phone){
        OrgUser orgUser = null;
        try {
            orgUser = orgUserMapper.selectOne(new QueryWrapper<OrgUser>().eq("phone", phone));
        }catch (Exception e){
            e.printStackTrace();
        }
        return orgUser == null ? false : true;
    }

    /**
     * @Description  写入用户数据信息
     * @Param [dto]
     * @return void
     **/
    @Override
    public void insertOrgUser(RegisterDto dto) {
        OrgUser user = new OrgUser();
        BeanUtils.copyProperties(dto,user);
        orgUserMapper.insert(user);
    }
}
